package com.xinchuang.service.targetSystem.Impl.indexUtil;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinchuang.comment.R;
import com.xinchuang.entity.target.TargetBaseData;
import com.xinchuang.entity.target.TargetSpecialAttribute;
import com.xinchuang.entity.targetSystem.system.TargetSubType;
import com.xinchuang.entity.targetSystem.system.TargetType;
import com.xinchuang.entity.targetSystem.target.TargetSubRelationTable;
import com.xinchuang.mapper.target.TargetBaseDataMapper;
import com.xinchuang.mapper.target.TargetSpecialAttributeMapper;
import com.xinchuang.mapper.targetSystem.MyUtilMapper;
import com.xinchuang.mapper.targetSystem.TargetSubTypeMapper;
import com.xinchuang.mapper.targetSystem.TargetTypeMapper;
import com.xinchuang.mapper.targetSystem.target.TargetSubRelationTableMapper;
import com.xinchuang.pojoUtil.TargetUtil;
import com.xinchuang.service.RedisUseService;
import com.xinchuang.service.targetSystem.indexUtil.IndexPullService;
import com.xinchuang.util.RedisUtil;
import com.xinchuang.utils.RedisName;
import com.xinchuang.vo.FindTargetAndSubType;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.swing.plaf.ListUI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author zkwczx
 * @version 1.0
 * @date 2024/4/18 9:28
 */
@Service
@RequiredArgsConstructor
public class IndexPullServiceImpl implements IndexPullService {

    private final RedisUtil redisUtil;

    private final TargetTypeMapper targetTypeMapper;

    private final RedisUseService redisUseService;

    private final TargetSubTypeMapper targetSubTypeMapper;

    private final TargetBaseDataMapper targetBaseDataMapper;

    private final TargetSubRelationTableMapper targetSubRelationTableMapper;

    private final MyUtilMapper my;

    private final TargetSpecialAttributeMapper targetSpecialAttributeMapper;

    @Override
    public List<Map<String, Object>> pullChoose(String id, String type) {
        if (StringUtils.isEmpty(type)) {
            type = "";
        }
        switch (type) {
            case "selectValue":
                return getKeyShowType(id, "select");
            case "targetType":
            case "targetQuantity":
                return getSubType(id);
            case "targetDistance":
            case "targetPositionDistance":
                return getTargetType(id);
            default:
                return getKeyTypeDouble(id, "Double");
        }
    }

    private List<Map<String, Object>> getKeyShowType(String id, String showType) {
        String typeId = null;
        boolean isTarget = true;
        if (redisUtil.hHasKey(RedisName.SIGN_TARGET_NAME_ALL, id)) {
            typeId = id;
        } else if (redisUtil.hHasKey(RedisName.SIGN_SUB_NAME_ALL, id)){
            typeId = id;
            isTarget = false;
        }
        if (null == typeId) {
            TargetBaseData baseData = targetBaseDataMapper.selectById(id);
            if (ObjectUtil.isNotNull(baseData)) {
                typeId = baseData.getType();
            } else {
                TargetSubRelationTable sub = targetSubRelationTableMapper.selectOne(new LambdaQueryWrapper<TargetSubRelationTable>().eq(TargetSubRelationTable::getSubId, id));
                if (ObjectUtil.isNotNull(sub)) {
                    Map<Object, Object> baseMap = redisUtil.hmget(RedisName.TARGET_BASE_DATA_ALL + "_" + id);
                    typeId = TargetUtil.getObjectListOrString(baseMap.get("targetClassify"));
                    isTarget = false;
                } else {
                    return ListUtil.empty();
                }
            }
        }
        if (isTarget) {
            List<FindTargetAndSubType> targetAndSubType = my.findTargetType(typeId);
            if (CollectionUtil.isNotEmpty(targetAndSubType)) {
                List<Map<String, Object>> resultList = new ArrayList<>();
                String middle = null;
                Map<String, Object> map = new HashMap<>();
                List<Map<String, Object>> list = new ArrayList<>();
                for (FindTargetAndSubType findTargetAndSubType : targetAndSubType) {
                    if (null == middle || !middle.equals(findTargetAndSubType.getTypeId())) {
                        if (CollectionUtil.isNotEmpty(list)) {
                            map.put("children", list);
                            resultList.add(map);
                        }
                        middle = findTargetAndSubType.getTypeId();
                        map = new HashMap<>();
                        map.put("value", findTargetAndSubType.getTypeId());
                        map.put("label", findTargetAndSubType.getTypeName() + "(目标)");
                        list = new ArrayList<>();
                    }
                    if (showType.equals(findTargetAndSubType.getShowType())) {
                        Map<String, Object> typeData = new HashMap<>();
                        typeData.put("label", findTargetAndSubType.getKeyName());
                        typeData.put("value", findTargetAndSubType.getId());
                        list.add(typeData);
                    }
                }
                if (CollectionUtil.isNotEmpty(list)) {
                    map.put("children", list);
                    resultList.add(map);
                }
                return resultList;
            }
        } else {
            List<FindTargetAndSubType> subTypes = my.findSubType(typeId);
            if (CollectionUtil.isNotEmpty(subTypes)) {
                List<Map<String, Object>> resultList = new ArrayList<>();
                Map<String, Object> map = new HashMap<>();
                List<Map<String, Object>> list = new ArrayList<>();
                for (FindTargetAndSubType subType : subTypes) {
                    if (CollectionUtil.isEmpty(map)) {
                        map.put("value", subType.getTypeId());
                        map.put("label", subType.getTypeName() + "(子目标)");
                    }
                    if (showType.equals(subType.getShowType())) {
                        Map<String, Object> typeData = new HashMap<>();
                        typeData.put("label", subType.getKeyName());
                        typeData.put("value", subType.getId());
                        list.add(typeData);
                    }
                }
                if (CollectionUtil.isNotEmpty(list)) {
                    map.put("children", list);
                    resultList.add(map);
                }
                return resultList;
            }
        }
        return ListUtil.empty();
    }

    private List<Map<String, Object>> getKeyTypeDouble(String id, String keyType) {
        String typeId = null;
        boolean isTarget = true;
        if (redisUtil.hHasKey(RedisName.SIGN_TARGET_NAME_ALL, id)) {
            typeId = id;
        } else if (redisUtil.hHasKey(RedisName.SIGN_SUB_NAME_ALL, id)){
            typeId = id;
            isTarget = false;
        }
        if (null == typeId) {
            TargetBaseData baseData = targetBaseDataMapper.selectById(id);
            if (ObjectUtil.isNotNull(baseData)) {
                typeId = baseData.getType();
            } else {
                TargetSubRelationTable sub = targetSubRelationTableMapper.selectOne(new LambdaQueryWrapper<TargetSubRelationTable>().eq(TargetSubRelationTable::getSubId, id));
                if (ObjectUtil.isNotNull(sub)) {
                    Map<Object, Object> baseMap = redisUtil.hmget(RedisName.TARGET_BASE_DATA_ALL + "_" + id);
                    typeId = TargetUtil.getObjectListOrString(baseMap.get("targetClassify"));
                    isTarget = false;
                } else {
                    return ListUtil.empty();
                }
            }
        }
        if (isTarget) {
            List<FindTargetAndSubType> targetAndSubType = my.findTargetAndSubType(typeId);
            if (CollectionUtil.isNotEmpty(targetAndSubType)) {
                List<Map<String, Object>> resultList = new ArrayList<>();
                String middle = null;
                Map<String, Object> map = new HashMap<>();
                List<Map<String, Object>> list = new ArrayList<>();
                for (FindTargetAndSubType findTargetAndSubType : targetAndSubType) {
                    if (null == middle || !middle.equals(findTargetAndSubType.getTypeId())) {
                        if (CollectionUtil.isNotEmpty(list)) {
                            map.put("children", list);
                            resultList.add(map);
                        }
                        middle = findTargetAndSubType.getTypeId();
                        map = new HashMap<>();
                        map.put("value", findTargetAndSubType.getTypeId());
                        if (1 == findTargetAndSubType.getStatusType()) {
                            map.put("label", findTargetAndSubType.getTypeName() + "(目标)");
                        } else {
                            map.put("label", findTargetAndSubType.getTypeName() + "(子目标)");
                        }
                        list = new ArrayList<>();
                    }
                    if (keyType.equals(findTargetAndSubType.getKeyType())) {
                        Map<String, Object> typeData = new HashMap<>();
                        typeData.put("label", findTargetAndSubType.getKeyName());
                        typeData.put("value", findTargetAndSubType.getId());
                        list.add(typeData);
                    }
                }
                if (CollectionUtil.isNotEmpty(list)) {
                    map.put("children", list);
                    resultList.add(map);
                }
                return resultList;
            }
        } else {
            List<FindTargetAndSubType> subTypes = my.findSubType(typeId);
            if (CollectionUtil.isNotEmpty(subTypes)) {
                List<Map<String, Object>> resultList = new ArrayList<>();
                Map<String, Object> map = new HashMap<>();
                List<Map<String, Object>> list = new ArrayList<>();
                for (FindTargetAndSubType subType : subTypes) {
                    if (CollectionUtil.isEmpty(map)) {
                        map.put("value", subType.getTypeId());
                        map.put("label", subType.getTypeName() + "(子目标)");
                    }
                    if (keyType.equals(subType.getKeyType())) {
                        Map<String, Object> typeData = new HashMap<>();
                        typeData.put("label", subType.getKeyName());
                        typeData.put("value", subType.getId());
                        list.add(typeData);
                    }
                }
                if (CollectionUtil.isNotEmpty(list)) {
                    map.put("children", list);
                    resultList.add(map);
                }
                return resultList;
            }
        }
        return ListUtil.empty();
    }

    private List<Map<String, Object>> getTargetType(String id) {
        if (redisUtil.hHasKey(RedisName.SIGN_TARGET_NAME_ALL, id)) {
            return getTargetType();
        }
        TargetBaseData baseData = targetBaseDataMapper.selectById(id);
        if (ObjectUtil.isNotNull(baseData)) {
            return getTargetType();
        }
        return ListUtil.empty();
    }

    private List<Map<String, Object>> getTargetType() {
        List<TargetType> types = targetTypeMapper.selectList(new LambdaQueryWrapper<TargetType>().ne(TargetType::getTypeId, "0"));
        if (CollectionUtil.isNotEmpty(types)) {
            List<Map<String, Object>> resultList = new ArrayList<>();
            for (TargetType type : types) {
                Map<String, Object> map = new HashMap<>();
                map.put("value", type.getTypeId());
                map.put("label", type.getTypeName() + "(目标)");
                resultList.add(map);
            }
            return resultList;
        } else {
            return ListUtil.empty();
        }
    }

    private List<Map<String, Object>> getSubType(String id) {
        String typeId = null;
        if (redisUtil.hHasKey(RedisName.SIGN_TARGET_NAME_ALL, id)) {
            typeId = id;
        } else {
            TargetBaseData baseData = targetBaseDataMapper.selectById(id);
            if (ObjectUtil.isNotNull(baseData)) {
                typeId = baseData.getType();
            } else {
                return ListUtil.empty();
            }
        }
        List<TargetSubType> targetSubTypes = targetSubTypeMapper.selectList(new LambdaQueryWrapper<TargetSubType>().eq(TargetSubType::getParentId, typeId));
        if (CollectionUtil.isNotEmpty(targetSubTypes)) {
            List<Map<String, Object>> resultList = new ArrayList<>();
            for (TargetSubType type : targetSubTypes) {
                Map<String, Object> map = new HashMap<>();
                map.put("value", type.getTypeId());
                map.put("label", type.getTypeName() + "(子目标)");
                resultList.add(map);
            }
            return resultList;
        } else {
            return ListUtil.empty();
        }
    }

    @Override
    public R selectSubTypeKeyValue(String subTypeId, String keyType) {
        List<FindTargetAndSubType> targetAndSubType = my.findSubType(subTypeId);
        if (CollectionUtil.isNotEmpty(targetAndSubType)) {
            List<Map<String, Object>> list = new ArrayList<>();
            for (FindTargetAndSubType type : targetAndSubType) {
                if (keyType.equals(type.getKeyType())) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("value", type.getId());
                    map.put("label", type.getKeyName());
                    list.add(map);
                }
            }
            return R.data(list);
        }
        return R.data(ListUtil.empty());
    }

    @Override
    public R selectSubTypeShowType(String subTypeId) {
        TargetSpecialAttribute attribute = targetSpecialAttributeMapper.selectById(subTypeId);
        if (ObjectUtil.isNotNull(attribute)) {
            JSONArray array = JSONArray.parseArray(attribute.getEnumerator());
            if (CollectionUtil.isNotEmpty(array)) {
                List<Map<String, Object>> list = new ArrayList<>();
                for (int i = 0; i < array.size(); i++) {
                    JSONObject object = array.getJSONObject(i);
                    Map<String, Object> map = new HashMap<>();
                    map.put("selectValue", object.getString("label"));
                    map.put("score", 0);
                    list.add(map);
                }
                return R.data(list);
            }
        }
        return R.data(ListUtil.empty());
    }

}
